using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace CodePlex.Resourceful
{

    public class NameValuePairs : Dictionary<string,string>
    {
        public NameValuePairs() : base() { }

        public NameValuePairs(NameValuePairs other) : base(other) {  }

        public NameValuePairs(IEqualityComparer<string> comparer) : base(comparer) { }


        public static NameValuePairs From(params string[] namesAndValues)
        {
            NameValuePairs rt = new NameValuePairs();
            if (namesAndValues == null || namesAndValues.Length == 0)
            {
                return rt;
            }

            if (namesAndValues.Length % 2 == 1)
            {
                throw new ArgumentException("Must pass an even number of arguments (names and values)", "namesAndValues");
            }
            for (int i = 0; i < namesAndValues.Length; i += 2)
            {
                rt[namesAndValues[i]] = namesAndValues[i + 1];
            }
            return rt;

        }

#if SILVERLIGHT

        public static NameValuePairs From(System.Net.WebHeaderCollection values)
        {
            NameValuePairs rt = new NameValuePairs();
            foreach (string key in values.Headers)
            {
                rt[key] = values[key];
            }
            return rt;
        }

#else
        public static NameValuePairs From(System.Collections.Specialized.NameValueCollection values)
        {
            NameValuePairs rt = new NameValuePairs();
            foreach (string key in values.AllKeys)
            {
                rt[key] = values[key];
            }
            return rt;
        }
#endif


        public void SetIfNotExists(string name, string value)
        {
            if (!this.ContainsKey(name))
            {
                this[name] = value;
            }
        }

        public string GetIfExists(string name)
        {
            if (!this.ContainsKey(name))
            {
                return null;
            }
            return this[name];
        }

        public override string ToString()
        {
            return Fn.Join(",", this);
        }


        public bool ContainsKeys(params string[] keys)
        {
            foreach (string key in keys)
            {
                if (!this.ContainsKey(key))
                {
                    return false;
                }
            }
            return true;
        }


        public IEnumerable<TOutput> ConvertAll<TOutput>(Converter<KeyValuePair<string, string>, TOutput> converter)
        {
            return Fn.Select(this, converter);
        }


        public void AddRange(IEnumerable<KeyValuePair<string, string>> values)
        {
            foreach (KeyValuePair<string, string> value in values)
            {
                this.Add(value.Key, value.Value);
            }
        }

    }
}
